<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/28 0028
 * Time: 上午 10:47
 */

namespace app\sports_admin\service;

use app\exception\FeedBackException;
use app\sports_admin\model\FeedBack as FeedBackModel;
use app\sports_admin\model\FeedbackReply as FeedbackReplyModel;
use think\Db;

class Feedback extends Base
{
    /**
     * 反馈详情
     */
    public static function detail()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        return FeedBackModel::detail($params['id'])->toArray();
    }
    /**
     * 反馈回复
     */
    public static function reply()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = FeedBackModel::where('id',$params['id'])->where('state',2)->find();
        if($bool){
            throw new FeedBackException(['msg'=>'已回复的反馈不可以再次回复','errorCode'=>400]);
        }
        Db::startTrans();
        $bool = (new FeedBackModel())->save(['state'=>2],['id'=>$params['id']]);
        if(!$bool){
            Db::rollback();
            throw new FeedBackException(['msg'=>'回复失败','errorCode'=>400]);
        }
        $bool = FeedbackReplyModel::create(['feedback_id'=>$params['id'],'contents'=>$params['contents']]);
        if(!$bool){
            Db::rollback();
            throw new FeedBackException(['msg'=>'回复失败','errorCode'=>400]);
        }
        Db::commit();
        return json(['msg'=>'回复成功','code'=>200]);
    }
    /**
     * 删除
     */
    public static function delete()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = FeedBackModel::destroy($params['id']);
        if(!$bool){
            throw new FeedBackException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['msg'=>'删除成功','code'=>200]);
    }
}